﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace Console
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("用户名和密码不能为空");
                return;
            }
            if (!(username == "root"))
            {
                string account_path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "accounts.txt";
                if (!File.Exists(account_path))
                {
                    File.Create(account_path).Close();
                }

                string[] lines = File.ReadAllLines(account_path);
                foreach (string line in lines)
                {
                    string[] parts = line.Split(',');
                    if (parts[0] == username)
                    {
                        MessageBox.Show("用户名已存在");
                        return;
                    }
                }
                string hashedPassword = HashPassword(password);
                string lineToWrite = username + "," + hashedPassword;
                File.AppendAllLines(account_path, new[] { lineToWrite });

                MessageBox.Show("注册成功");
            }
            else
            {
                string admin_path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "admin.txt";
                if (!File.Exists(admin_path))
                {
                    File.Create(admin_path).Close();
                }
                File.WriteAllText(admin_path, HashPassword(password));
                MessageBox.Show("管理员密码修改成功");
            }


        }

        private string HashPassword(string password)
        {
            using (SHA256 sha256Hash = SHA256.Create())
            {
                byte[] data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.Append(data[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            // 计算Label和TextBox的中心高度差
            int delta1 = (label1.Height - textBox1.Height) / 2;
            int delta2 = (label2.Height - textBox2.Height) / 2;

            // 设置TextBox的Top属性，使其与Label的中心对齐
            textBox1.Top = label1.Top + delta1;
            textBox2.Top = label2.Top + delta2;

            // 设置textBox的Left属性，使其在窗体中居中
            textBox1.Left = (ClientSize.Width - textBox1.Width) / 2;
            label1.Left = textBox1.Left - label1.Width;
            textBox2.Left = (ClientSize.Width - textBox2.Width) / 2; ;
            label2.Left = textBox2.Left - label2.Width;

            // 提交按钮居中
            button1.Left = (ClientSize.Width - button1.Width) / 2;
        }
    }
}
